//
//  RMMSkillHeadView.m
//  Romomo
//
//  Created by puman on 2017/2/7.
//  Copyright © 2017年 mazegong. All rights reserved.
//

#import "RMMSkillHeadView.h"

@implementation RMMSkillHeadView
{
    UIImageView *_imageV;
    UILabel *_nameLabel;
    UILabel *_yearLabel;
    UILabel *_titleLabel;
    UILabel *_scoreLabel;
    NSMutableDictionary *_widthDic;
    NSMutableArray *_tipsArray;
    NSInteger _rowCount;

}
- (instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        self.backgroundColor = [UIColor whiteColor];
        _imageV = [[UIImageView alloc] initWithFrame:CGRectMake(kScreenWideth / 2.0 - 75 / 2.0, 18, 75, 75)];
        _imageV.layer.cornerRadius = 75 / 2.0;
        _imageV.layer.masksToBounds = YES;
        [self addSubview:_imageV];
        
        _nameLabel = [[UILabel alloc] initWithFrame:CGRectMake(_imageV.left, _imageV.bottom + 12, 50, 20)];
        _nameLabel.font = kBFont(16);
        [self addSubview:_nameLabel];
        
        _yearLabel = [[UILabel alloc] initWithFrame:CGRectMake(_nameLabel.right + 8, _nameLabel.top + 1, 30, 18)];
        _yearLabel.textColor = kWhiteColor;
        _yearLabel.backgroundColor = RGBA(236, 194, 66, 1);
        _yearLabel.textAlignment = NSTextAlignmentCenter;
        _yearLabel.font = kBFont(12);
        _yearLabel.layer.masksToBounds = YES;
        _yearLabel.layer.cornerRadius = 3;
        [self addSubview:_yearLabel];
        
        CGFloat h = 50 * (375.0 / kScreenWideth);
        _titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(15, _nameLabel.bottom, kScreenWideth - 30, h)];
        _titleLabel.font = kFont(12);
        _titleLabel.numberOfLines = 0;
        _titleLabel.textAlignment = NSTextAlignmentCenter;
        [self addSubview:_titleLabel];
        
        for (int i = 0; i < 5; i++) {
            UIImageView *imageV = [[UIImageView alloc] initWithFrame:CGRectMake((kScreenWideth - 110) / 2 + 16 * i, _titleLabel.bottom, 12, 12)];
            imageV.tag = 10000 + i;
            imageV.image = [UIImage imageNamed:@"星1-2@3x"];
            [self addSubview:imageV];
        }
        
        _scoreLabel = [[UILabel alloc] initWithFrame:CGRectMake((kScreenWideth - 110) / 2 + 80, _titleLabel.bottom, 40, 12)];
        _scoreLabel.textColor = COLORFROMCODE(0x9B9B9B , 1);
        _scoreLabel.font = kFont(10);
        [self addSubview:_scoreLabel];
        for (int i = 0; i < 100; i++) {
            UIButton *button = [UIButton buttonWithType:(UIButtonTypeCustom)];
            button.frame = CGRectMake(15, _scoreLabel.bottom + kMaric, 50, 25);
            button.layer.cornerRadius = 12.5;
            button.layer.masksToBounds = YES;
            button.hidden = YES;
            button.tag = 1000 + i;
            button.layer.borderWidth = 1;
            [button setTitleColor:kBasicColor forState:(UIControlStateNormal)];
            button.titleLabel.font = kFont(12);
            button.layer.borderColor = kBasicColor.CGColor;
            [self addSubview:button];
        }
    }
    return self;
}
- (void)setArray:(NSArray *)array{
    _rowCount = 0;
    _tipsArray = [NSMutableArray array];
    for (NSString *key in array) {
        [_tipsArray addObject:key];
    }
    [self setButTipsFrameWith:array andBegainIndex:0];
}
- (void)setButTipsFrameWith:(NSArray *)array andBegainIndex:(NSInteger)begianNndex{
    NSMutableArray *arr = [NSMutableArray array];
    for (NSString *key in array) {
        [arr addObject:key];
    }
    CGFloat width =  [self buttonTotalWidth:arr];
    if (30 + (arr.count - 1) * 10 + width <= self.width) {
        [self layoutButFramWith:arr with:width index:begianNndex];
    } else {
        CGFloat moreWidth = [self titleLabelWidth:@"展开查看更多"];
        CGFloat widths = 30 + moreWidth + 10;
        NSInteger index = 0;
        NSMutableArray *titleArray = [NSMutableArray array];
        for (int i = 0; i < arr.count; i++) {
            NSString *string = arr[i];
            if (i == 0) {
                widths += [self titleLabelWidth:string];
            } else {
                widths += [self titleLabelWidth:string] + 10;
            }
            index = i;
            [titleArray addObject:string];
            if (widths > self.width) {
                break;
            }
        }
        [titleArray removeObjectAtIndex:index];
        [titleArray addObject:@"展开查看更多"];
        UIButton *moreBut = [self viewWithTag:1000 + index];
        NSDictionary *dic = [NSDictionary dictionaryWithObject:[NSString stringWithFormat:@"%ld",index] forKey:@"index"];
        NS_POST(@"ZHANGKAIGENGDUO", nil, dic);
        [moreBut setImage:[UIImage imageNamed:@"moredown@3x"] forState:(UIControlStateNormal)];
        [self layoutButFramWith:titleArray with:[self buttonTotalWidth:titleArray] index:begianNndex];
        _rowCount = 1;
        for (int i = 0; i < index; i++) {
            [_tipsArray removeObjectAtIndex:0];
            _rowCount += 1;
        }
        begianNndex += 1;
        _index = begianNndex;
        [self normalStatusWith:_tipsArray andBegainIndex:begianNndex];
    }
}
- (void)normalStatusWith:(NSArray *)array andBegainIndex:(NSInteger)begianNndex{
    CGFloat width =  [self buttonTotalWidth:array];
    if (30 + (array.count - 1) * 10 + width <= self.width) {
        [self layoutButFramWith:array with:width index:begianNndex];
    } else {
        CGFloat widths = 15;
        NSInteger index = 0;
        NSMutableArray *titleArray = [NSMutableArray array];
        for (int i = 0; i < array.count; i++) {
            NSString *string = array[i];
            if (i == 0) {
                widths += [self titleLabelWidth:string];
            } else {
                widths += [self titleLabelWidth:string] + 10;
            }
            index = i;
            [titleArray addObject:string];
            if (widths > self.width) {
                break;
            }
        }
        [titleArray removeObjectAtIndex:index];
        [self layoutButFramWith:titleArray with:[self buttonTotalWidth:titleArray] index:begianNndex];
        for (int i = 0; i < index; i++) {
            [_tipsArray removeObjectAtIndex:0];
            _rowCount += 1;
        }
        begianNndex += 1;
        _index = begianNndex;
        [self normalStatusWith:_tipsArray andBegainIndex:begianNndex];
    }
}
- (void)layoutButFramWith:(NSArray *)array with:(CGFloat)width index:(NSInteger)begainIndex{
    CGFloat spacing = 0.0;
    if (array.count < 2) {
        spacing = 0.0;
    } else {
        spacing = (self.width - 30 - width) / (array.count - 1);
    }
    CGFloat x = 15;
    for (int i = 0; i < array.count; i++) {
        if ( i > 0) {
            x = x + [self titleLabelWidth:array[i - 1]] + spacing;
        }
        UIButton *button = [self viewWithTag:1000 + i + _rowCount];
        button.frame = CGRectMake(x , _scoreLabel.bottom + kMaric + 35 * begainIndex, [self titleLabelWidth:array[i]], 25);
        if (_rowCount == 0) {
            button.hidden = NO;
        } else {
            button.hidden = YES;
        }
        [button setTitle:array[i] forState:(UIControlStateNormal)];
    }
}
- (CGFloat)buttonTotalWidth:(NSArray *)array{
    CGFloat width = 0.0;
    for (int i = 0; i < array.count; i++) {
        NSString *string = array[i];
        width += [self titleLabelWidth:string];
    }
    return width;
}
- (CGFloat)titleLabelWidth:(NSString *)string{
    return [UILabel backHeightWithString:string CGFloatCount:25 metho:WIDTH font:12] + 20;
}
- (void)setDic:(NSDictionary *)dic{
    _scoreLabel.text = [NSString stringWithFormat:@"%@分",[RMMFileManager retrunObjWith:dic[@"EVALUATE_DETAILS"][@"AVERAGE_SCORE"]]];
    _titleLabel.text =  [RMMFileManager retrunObjWith:dic[@"EMP_DESCRIBE"]];
    CGFloat titleWidth = [UILabel backHeightWithString:_titleLabel.text CGFloatCount:kScreenWideth - 30 metho:HEIGHT font:12];
    
    _nameLabel.text = [RMMFileManager retrunObjWith:dic[@"EMPLOYEE_NAME"]];
    _yearLabel.text =  [NSString stringWithFormat:@"%@年",[RMMFileManager retrunObjWith:dic[@"ANNUAL_LEAVE"]]];
    [_imageV sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",kBaseImageUrl,[RMMFileManager retrunObjWith:dic[@"EMP_IMG"]]]] placeholderImage:[UIImage imageNamed:@"加载中@3x"]];
    CGFloat width = [UILabel backHeightWithString:_nameLabel.text CGFloatCount:25 metho:WIDTH font:16];
    _nameLabel.frame = CGRectMake(_imageV.left, _imageV.bottom + 12, width + 10, 25);
    CGFloat widthY = [UILabel backHeightWithString:_yearLabel.text CGFloatCount:18 metho:WIDTH font:12];
    _yearLabel.frame =  CGRectMake(_nameLabel.right , _nameLabel.top + 3.5, widthY + 10, 18);
    [self setStarWith:[RMMFileManager retrunObjWith:dic[@"EVALUATE_DETAILS"][@"AVERAGE_SCORE"]]];
}
- (void)setStarWith:(NSString *)score{
    NSInteger s1 = [score integerValue];
    CGFloat s2 = [score floatValue];
    for (int i = 0; i < 5; i++) {
        UIImageView *imageV = [self viewWithTag:10000 + i];
        if (i < s1) {
            imageV.image = [UIImage imageNamed:@"星-1@3x"];
        } else {
            if (i >= s1 && i < s2) {
                imageV.image = [UIImage imageNamed:@"星-3@3x"];
            } else {
                imageV.image = [UIImage imageNamed:@"星1-2@3x"];
            }
            
        }
    }
}
- (void)moreButClick{
    
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
